Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
"this" isn't working as I expected using string concatenation in Javascript

const user1 = {
    name: 'Sherlock Holmes',
    address:{
        street:'Baker street',
        number:"221B",
    },
    sayMyAddress:function (){
        console.log(`My address is ${this.address}`)
        console.log(this.address)
    }

}

user1.sayMyAddress()

The result:

My address is [object Object]

{ street: 'Baker street', number: '221B' }

Why the this keyword shows the address value in the second case and doesn't in the first? the scope in this case isn't in the object user?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The result of the conversion of the address to a string is '[object Object]' as we'd expect. For example, if you do

console.log({}.toString()) 

you'll get an output of:

[object Object]

A small change in the code will give the desired result:

const user1 = {
  name: 'Sherlock Holmes',
  address: {
    street: 'Baker street',
    number: "221B",
  },
  sayMyAddress: function() {
    console.log(`My address is ${this.address.number} ${this.address.street}`)
  }

}

user1.sayMyAddress()
.as-console-wrapper { max-height: 100% !important; }

You could also add a toString() function on the address object:

const user1 = {
  name: 'Sherlock Holmes',
  address: {
    street: 'Baker street',
    number: "221B",
    toString() { return `${this.number} ${this.street}` }
  },
  sayMyAddress: function() {
    console.log(`My address is ${this.address}`)
  }

}

user1.sayMyAddress()
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Relatório

0

this.address is an object, so even if the console.log is sympatic enough to output the full object with values if you pass it the object directly, if you use it in a string you'll get [object Object].

Just try it like this

console.log(`My address is ${this.address.street} ${this.address.number}`)
about 4 years ago · Juan Pablo Isaza Relatório

0

You are trying to log the object itself. So the output result is pretty much expected.

Change the log line from:

console.log(`My address is ${this.address}`)

to:

console.log(`My address is ${this.address.street} ${this.address.number}`)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda